package com.motong.gongdan.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motong.gongdan.config.ResponseData;
import com.motong.gongdan.config.auth.UserLoginToken;
import com.motong.gongdan.entity.LampType;
import com.motong.gongdan.entity.LampTypeNotice;
import com.motong.gongdan.entity.LampTypeUser;
import com.motong.gongdan.service.LampTypeNoticeService;
import com.motong.gongdan.service.LampTypeService;
import com.motong.gongdan.service.LampTypeUserService;
import com.motong.gongdan.util.LoginUtil;
import com.motong.gongdan.vo.system.LampType.query.LampTypeListQuery;
import com.motong.gongdan.vo.system.LampType.vo.LampTypeListVo;
import com.motong.gongdan.vo.system.common.query.IdValueQuery;
import com.motong.gongdan.vo.system.common.vo.CheckBox;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "联络灯类型")
@RestController
@RequestMapping("/sys/lampType")
public class LampTypeController {

    @Resource
    private LampTypeService lampTypeService;

    @Resource
    private LampTypeNoticeService lampTypeNoticeService;

    @Resource
    private LampTypeUserService lampTypeUserService;

    @ApiOperation("联络灯类型列表")
    @PostMapping("/getList")
    @UserLoginToken
    public ResponseData<List<LampTypeListVo>> getList(@RequestBody(required = false) LampTypeListQuery query) {
        query = Optional.ofNullable(query).orElse(new LampTypeListQuery());
        Page<LampTypeListVo> data = lampTypeService.getList(query);
        return ResponseData.success(data.getRecords(), data.getTotal());
    }


    @ApiOperation("添加联络灯类型")
    @PostMapping("/edit")
    @Transactional
    @UserLoginToken
    public ResponseData<String> edit(@RequestBody LampType lampType) {
        boolean isUpdate = Objects.nonNull(lampType.getId());
        lampType.setUpdateTime(new Date()).setUpdateUser(LoginUtil.getLoginUserId());
        if (!isUpdate){
            lampType.setCreateTime(new Date()).setCreateUser(LoginUtil.getLoginUserId());
        }
        if (!CollectionUtils.isEmpty(lampType.getRemoveLampTypeNoticeIds()) && isUpdate) {
            lampTypeUserService.remove(new LambdaQueryWrapper<LampTypeUser>().in(LampTypeUser::getLampTypeNoticeId, lampType.getRemoveLampTypeNoticeIds()));
            lampTypeNoticeService.removeByIds(lampType.getRemoveLampTypeNoticeIds());
        }
        lampTypeService.saveOrUpdate(lampType);
        if (!CollectionUtils.isEmpty(lampType.getLampTypeNotices())) {
            Set<Long> noticeIds = lampType.getLampTypeNotices().stream().map(LampTypeNotice::getId).collect(Collectors.toSet());
            if (!CollectionUtils.isEmpty(noticeIds)){
                lampTypeUserService.remove(new LambdaQueryWrapper<LampTypeUser>().in(LampTypeUser::getLampTypeNoticeId, noticeIds));
            }
            lampType.getLampTypeNotices().forEach(i->{
                i.setLampTypeId(lampType.getId()).setCreateTime(new Date()).setCreateUser(LoginUtil.getLoginUserId());
                lampTypeNoticeService.saveOrUpdate(i);
                if (!CollectionUtils.isEmpty(i.getUserIdList())){
                    List<LampTypeUser> lampTypeUsers = i.getUserIdList().stream().map(d -> new LampTypeUser().setUserId(d).setLampTypeId(lampType.getId()).setCreateTime(new Date())
                            .setLampTypeNoticeId(i.getId())).collect(Collectors.toList());
                    lampTypeUserService.saveBatch(lampTypeUsers);
                }
            });
        }
        return ResponseData.success("编辑成功!");
    }

    @ApiOperation("删除联络灯类型")
    @PostMapping("/del")
    @UserLoginToken
    @Transactional
    public ResponseData<String> del(@RequestBody IdValueQuery query) {
        if (!CollectionUtils.isEmpty(query.getIdList())) {
            lampTypeUserService.remove(new LambdaQueryWrapper<LampTypeUser>().in(LampTypeUser::getLampTypeId, query.getIdList()));
            lampTypeNoticeService.remove(new LambdaQueryWrapper<LampTypeNotice>().in(LampTypeNotice::getLampTypeId, query.getIdList()));
            lampTypeService.removeByIds(query.getIdList());
        }
        return ResponseData.success("删除成功!");
    }


    @ApiOperation("联络灯类型下拉列表")
    @PostMapping("/dropDown")
    @UserLoginToken
    public ResponseData<List<CheckBox>> dropDown(@RequestBody(required = false) LampTypeListQuery query) {
        query = Optional.ofNullable(query).orElse(new LampTypeListQuery());
        List<CheckBox> data = lampTypeService.dropDown(query);
        return ResponseData.success(data);
    }

}
